/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-05-12     lyh       the first version
 *  线程示例代码
 */
#include <rtdevice.h>
#include <rtthread.h>

/*
 * 定义线程句柄、线程堆栈大小、时间片参数、线程优先级
 */
#define THREAD_PRIORITY         25
#define THREAD_STACK_SIZE       512
#define THREAD_TIMESLICE        5
// 线程1句柄
static rt_thread_t tid1 = RT_NULL;
// 线程2句柄
static rt_thread_t tid2 = RT_NULL;

/*线程1入口函数*/
static void thread1_entry(void * params)
{
    rt_uint32_t count = 0;
    while (1)
    {
        /*线程1优先运行，循环打印count的值*/
        rt_kprintf("thread1 count:%d\n", count++);
        rt_thread_mdelay(500);
    }
}

/*线程2入口函数*/
static void thread2_entry(void * params)
{
    rt_uint32_t count = 0;
    while (count < 10)
    {
        rt_kprintf("thread2 count:%d\n", count++);
    }
    rt_kprintf("thread2 exit\n");
}

int thread_sample(void)
{
    tid1 = rt_thread_create("thread1",
            thread1_entry,
            RT_NULL,
            THREAD_STACK_SIZE,
            THREAD_PRIORITY,
            THREAD_TIMESLICE);

    if (tid1 !=RT_NULL) {
        rt_thread_startup(tid1);
    }

    tid2 = rt_thread_create("thread2",
            thread2_entry,
            RT_NULL,
            THREAD_STACK_SIZE,
            THREAD_PRIORITY,
            THREAD_TIMESLICE+1);

    if (tid2 !=RT_NULL) {
        rt_thread_startup(tid2);
    }

    return 0;
}
MSH_CMD_EXPORT(thread_sample,thread_sample)
